#!/bin/sh
#+---------------------------------------------------------------------------------+
#|script name: sysinfo_col.sh                                                      |
#|    funtion: This is a simple script  to collect system info, process info etc.  |
#|     auther: zsz150@qq.com				                                       |
#| build time: 2014-12-03                                                          |
#+---------------------------------------------------------------------------------+

export LANG=C

echo_split() {
    echo '#####################'
}

sys_uptime() {
	echo "########uptime#######"
	UPTIME=`UNIX95= ps -p 1 -oetime`
	echo $UPTIME
	echo_split
}

linux_user(){
	echo "########userinfo#######"
	cat /etc/passwd
	echo_split
	for USER in `cat /etc/passwd|grep "sh$"|awk -F: '{print $1}'`;
	do
		echo "$USER crontab list"
		echo_split
		crontab -u $USER -l|grep -v '\#'
	done
	echo_split
}

os_user(){
	echo "########userinfo#######"
	cat /etc/passwd
	echo_split
	cat /etc/passwd|grep "sh$"
	echo_split
	for USER in `cat /etc/passwd|grep "sh$"|awk -F: '{print $1}'`;
	do
		echo "$USER crontab list"
		echo_split
		crontab -l $USER |grep -v '\#'
	done
	echo_split
}

linux_network() {
	echo "########network#######"
	ifconfig -a;
	echo_split
	
	for file in `ls /etc/sysconfig/network-scripts/ifcfg-*`;
		do
		echo $file;
		echo_split;
		cat $file;
		echo_split;
		done
	echo_split;
	
	netstat -rn;
	echo_split;
	
	iptables -L -n;
	echo_split
	
	cat /etc/hosts|grep -v ^#
	echo_split
}


os_network() {
	echo "########network#######"
	netstat -in;
	echo_split;
	
	netstat -rn;
	echo_split;
	
	cat /etc/hosts|grep -v ^#
	echo_split
}

linux_disk() {
	echo "########diskinfo#######"
	df -hP
	echo_split
	cat /etc/fstab
}

hpux_disk() {
	echo "########diskinfo#######"
	bdf
}

aix_disk() {
	echo "########diskinfo#######"
	df -g
}

tomcat_list() {
	echo "########tomcat#######"
	ps -ef|grep -i tomcat|grep -v grep
	echo_split

	ps -ef|grep -v grep|grep -i tomcat|awk '{print $2}'|while read line;
	do
			TMP_DIR=`lsof -p $line|grep -i tomcat|grep 'common\/'|head -1|awk '{print $NF}'`;
			#TOMCAT_DIR=`expr match $TMP_DIR '\(\/.*tomcat.*\/\)'`
			#expr的方法有的时候在hpux下没用，调整了为更为通用的命令dirname
			TMP_DIR2=`dirname $TMP_DIR`
			TOMCAT_DIR=`dirname $TMP_DIR2`
			for WEBAPPS_DIR in `ls -ld $TOMCAT_DIR/../webapps/*|grep -vE 'examples|manager|ROOT|docs|META-INF|WEB-INF'|grep '^d'|awk '{print $NF}'`
			do
				echo $line,$WEBAPPS_DIR;
			done
			echo_split
	done
}

linux_process() {
	echo "########process#######"
	ps -ef|grep -i -E 'java|oracle|mqm|sybase'|grep -v grep
	echo_split
	
	ps -ef|grep -i -E 'java|oracle|mqm|sybase'|grep -v grep|awk '{print $2}'|\
	while read line;
	do
		pwdx $line;
	done
	echo_split
}
	
os_process() {
	echo "########process#######"
	ps -ef|grep -i -E 'java|oracle|mqm|sybase'|grep -v grep
	echo_split

	ps -ef|grep -i -E 'java|oracle|mqm|sybase'|grep -v grep|awk '{print $2}'|\
	while read line;
	do
		lsof -p $line|head -2|tail -1|awk '{print $NF}';
	done
	echo_split
}

linux_run() {
	uptime > $DIR/linux_sysinfo.txt
	linux_user >> $DIR/linux_sysinfo.txt
	linux_network >> $DIR/linux_sysinfo.txt
	linux_disk >> $DIR/linux_sysinfo.txt
	linux_process > $DIR/linux_process.txt
	tomcat_list > $DIR/tomcat_list.txt
}

hpux_run() {
	uptime > $DIR/hpux_sysinfo.txt
	os_user >> $DIR/hpux_sysinfo.txt
	os_network >> $DIR/hpux_sysinfo.txt
	aix_disk >> $DIR/hpux_sysinfo.txt
	os_process > $DIR/hpux_process.txt
	tomcat_list > $DIR/tomcat_list.txt
}

aix_run() {
	uptime > $DIR/aix_sysinfo.txt
	os_user >> $DIR/aix_sysinfo.txt
	os_network >> $DIR/aix_sysinfo.txt
	aix_disk >> $DIR/aix_sysinfo.txt
	os_process > $DIR/aix_process.txt
	tomcat_list > $DIR/tomcat_list.txt	
}

auto_run() {
	DIR=`date +%Y%m%d`_$IP
	mkdir $DIR
	UNAME=`uname`
	if [ "$UNAME" = "Linux" ];then
		linux_run
	elif [ "$UNAME" = "HP-UX" ];then
		hpux_run
	elif [ "$UNAME" = "AIX" ];then
		aix_run
	fi
	
	if [ `grep -v '^#' $DIR/tomcat_list.txt|wc -l|awk '{print $1}'` == 0 ];then
		rm -f $DIR/tomcat_list.txt
	fi

	tar -cvf $DIR.tar $DIR
	chmod 777 $DIR.tar
	rm -rf ./$DIR
}

if [ -r SYSINFO_IP.txt ];then
	IP=`cat SYSINFO_IP.txt`
	auto_run
else
	IP=$1
	if [ -z $IP ];then
		echo "sysinfo.txt is not exist,Please run the script agint with ip,\
		like this:./sysinfo.sh 127.0.0.1"
	else
		read -p "Please input Y/N to sure you IP is $IP:" yn
		sleep 1
		
		if [ "$yn" = "Y" -o "$yn" = "y" ];then
			`echo $IP > SYSINFO_IP.txt`
			auto_run
		else
			echo "Please run the script again! "
		fi
	fi
fi

